package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.components.CheckboxOptionRow
import com.promise.jokerdream.ui.theme.AppSpacing
import com.promise.jokerdream.ui.theme.AppFontSizes
import com.promise.jokerdream.database.entity.MercenaryDispatchGameConfigEntity
import com.promise.jokerdream.config.game.MercenaryTaskType

/**
 * 佣兵派遣设置卡片
 */
@Composable
fun MercenaryDispatchSettingCard(
    config: MercenaryDispatchGameConfigEntity,
    onMercenaryDispatchChange: (Boolean) -> Unit,
    onTaskTypeChange: (Set<MercenaryTaskType>) -> Unit,
    modifier: Modifier = Modifier,
    onExecuteClick: (() -> Unit)? = null
) {
    SettingCard(
        title = "佣兵派遣",
        description = "",
        checked = config.enabledSetting,
        onCheckedChange = onMercenaryDispatchChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (config.enabledSetting) {
                Column(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    val selectedTaskTypesSet = remember(config.selectedTaskTypes) {
                        if (config.selectedTaskTypes.isEmpty()) emptySet() else config.selectedTaskTypes.split(",").mapNotNull { taskTypeStr -> try { MercenaryTaskType.valueOf(taskTypeStr.trim()) } catch (e: Exception) { null } }.toSet()
                    }
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(vertical = AppSpacing.componentSmall),
                        horizontalArrangement = Arrangement.SpaceBetween,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        // S级任务选项
                        CheckboxOptionRow(
                            title = "S级任务",
                            checked = MercenaryTaskType.S_LEVEL in selectedTaskTypesSet,
                            onCheckedChange = { checked ->
                                val currentSet = if (config.selectedTaskTypes.isEmpty()) emptySet() else config.selectedTaskTypes.split(",").mapNotNull { taskTypeStr -> try { MercenaryTaskType.valueOf(taskTypeStr.trim()) } catch (e: Exception) { null } }.toSet()
                                val newSelectedTypes = if (checked) {
                                    currentSet + MercenaryTaskType.S_LEVEL
                                } else {
                                    currentSet - MercenaryTaskType.S_LEVEL
                                }
                                onTaskTypeChange(newSelectedTypes)
                            },
                            modifier = Modifier.weight(1f)
                        )
                        
                        // A级任务选项
                        CheckboxOptionRow(
                            title = "A级任务",
                            checked = MercenaryTaskType.A_LEVEL in selectedTaskTypesSet,
                            onCheckedChange = { checked ->
                                val currentSet = if (config.selectedTaskTypes.isEmpty()) emptySet() else config.selectedTaskTypes.split(",").mapNotNull { taskTypeStr -> try { MercenaryTaskType.valueOf(taskTypeStr.trim()) } catch (e: Exception) { null } }.toSet()
                                val newSelectedTypes = if (checked) {
                                    currentSet + MercenaryTaskType.A_LEVEL
                                } else {
                                    currentSet - MercenaryTaskType.A_LEVEL
                                }
                                onTaskTypeChange(newSelectedTypes)
                            },
                            modifier = Modifier.weight(1f)
                        )
                        
                        // B级任务选项
                        CheckboxOptionRow(
                            title = "B级任务",
                            checked = MercenaryTaskType.B_LEVEL in selectedTaskTypesSet,
                            onCheckedChange = { checked ->
                                val currentSet = if (config.selectedTaskTypes.isEmpty()) emptySet() else config.selectedTaskTypes.split(",").mapNotNull { taskTypeStr -> try { MercenaryTaskType.valueOf(taskTypeStr.trim()) } catch (e: Exception) { null } }.toSet()
                                val newSelectedTypes = if (checked) {
                                    currentSet + MercenaryTaskType.B_LEVEL
                                } else {
                                    currentSet - MercenaryTaskType.B_LEVEL
                                }
                                onTaskTypeChange(newSelectedTypes)
                            },
                            modifier = Modifier.weight(1f)
                        )
                    }
                }
            }
        }
    )
}
